﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Cms.Entity.Menu;
using Frame.Lib.Utilities;
using Cms.BLL.Menu;
using Frame.Lib.Enums;

namespace Cms.Admin.Menu
{
    public partial class Edit2 : PageEdit
    {
        private MenuBO _menu = new MenuBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (PKID != null && PKID != "")
                {
                    DispInfo();
                }
                else
                {
                    UpperID.Value = StrUtil.DecryptDES(Request["UpperID"]);
                    lblUpperName.Text = _menu.GetName(UpperID.Value);
                    Code.Text = _menu.GetMaxCode().ToString();
                    Orders.Text = Code.Text;
                }
            }
            AddAttr();
        }

        private void AddAttr()
        {

        }

        private void DispInfo()
        {
            MenuInfo info = _menu.GetInfo(PKID);

            EntityUtil.SetControl(pnlMain, info);

            if (info.UpperID == 0)
            {
                lblUpperName.Text = "顶级栏目没有上级栏目";
            }
            else
            {
                lblUpperName.Text = _menu.GetName(info.UpperID);
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            int upperID = int.Parse(UpperID.Value);
            MenuInfo upperInfo = _menu.GetInfo(upperID);

            bool blnFlag = _menu.Exist(upperID, Name.Text, PKID) || _menu.ExistCode(Code.Text, PKID);
            if (blnFlag)
            {
                JSUtil.Alert("该栏目或编号已存在");
            }
            else
            {
                MenuInfo info = _menu.GetInfo(PKID);
                if (Orders.Text.Trim() == "")
                {
                    Orders.Text = "0";
                }
                EntityUtil.SetEntity(pnlMain, info);
                if (string.IsNullOrEmpty(this.PKID))
                {
                    info.TypeID = upperInfo.TypeID;
                    info.ArticleSmallImageSize = upperInfo.ArticleSmallImageSize;
                    info.ArticleBigImageSize = upperInfo.ArticleBigImageSize;
                    info.PicSallImageSize = upperInfo.PicSallImageSize;
                    info.PicBigImageSize = upperInfo.PicBigImageSize;
                    info.PicSaveOrignImage = upperInfo.PicSaveOrignImage;
                }
                if (upperID != Constants.TREE_ROOT_UPPER_ID)//对于顶级栏目的处理
                {
                    MenuInfo parentInfo = _menu.GetInfo(upperID);
                    info.Depth = parentInfo.Depth + 1;
                    info.Path = parentInfo.Path + parentInfo.ID + ",";
                }
              
                _menu.Save(info);
                JSUtil.RefreshView();
            }
        }
    }
}
